<template>
  <div class="dropdown" ref="dropdownRef">
    <a
      href
      class="dropdown-toggle btn btn-outline-light"
      @click.prevent="toggleOpen"
      >{{ title }}</a
    >
    <ul v-if="isOpen" class="dropdown-menu">
      <slot></slot>
    </ul>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";
import useclickoutside from "@/share/hooks/useClickoutside";
import mitt from "mitt";
export const emitter = mitt();
export const pathName = ref("");
interface DrowdownItemType {
  path: string;
  name: string;
  disable: boolean;
}
export default defineComponent({
  name: "Dropdown",
  props: {
    title: {
      type: String,
      required: true,
    },
    activePath: {
      type: String,
      required: false,
    },
  },
  setup() {
    emitter.on("itemClick", function (path) {
      console.log("itemClick", path);
    });
    const isOpen = ref(false);
    const toggleOpen = () => {
      isOpen.value = true;
      pathName.value = location.pathname;
      emitter.emit("dropdownOpen");
    };
    const dropdownRef = ref<null | HTMLElement>(null);
    // 判断是否在区域外点击，来控制下拉列表的显示隐藏
    useclickoutside(dropdownRef, isOpen);
    return {
      isOpen,
      toggleOpen,
      dropdownRef,
    };
  },
});
</script>
<style lang="scss" scoped>
.dropdown {
  position: relative;
}
.dropdown-toggle {
  white-space: nowrap;
  &::after {
    display: inline-block;
    margin-left: 0.255em;
    vertical-align: 0.255em;
    content: "";
    border-top: 0.3em solid;
    border-right: 0.3em solid transparent;
    border-bottom: 0;
    border-left: 0.3em solid transparent;
  }
}
.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 99;
  display: block;
  min-width: 10rem;
  padding: 6px 0;
  margin: 0.25rem 0 0;
  font-size: 1rem;
  color: #212529;
  text-align: left;
  list-style: none;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 0.25rem;
}
</style>
